#!/bin/bash
# -*- encoding: utf8 -*-

from config.config import reviewfile, HiveHistoryfile
from BModel.globalfun import OrderCrontall


def AddDataInsertHive(db):
    print reviewfile
    fp = open(reviewfile,'r')
    lastid = fp.readline()
    fp.close()
    oldsql = "SELECT ID,ReviewID,Rating,OrderID FROM `bw_ctrip_review_cn_v2` WHERE ID >(%s) AND  Rating <= 4 "%(lastid)
    data = db.bmquery(oldsql,'ctripn')
    fp = open(HiveHistoryfile, 'a+')
    fp.write("新表新数据全部" + len(oldsql) + "条")
    insertsql,stip,maxid= "","",0
    print data['status']
    if True == data['status']:
        for v in data['data']:
            maxid = v['ID']
            import time
            time = time.strftime("%Y-%m-%d")
            insertsql = insertsql+stip +"(%s,'%s','%s)"%(v['ReviewID'], OrderCrontall(v['OrderID']), time)
            stip = ","

    if insertsql:
        fp.write("塞进新表新数据" + len(insertsql) + "条")
        hotelsql = "INSERT INTO BW_Order_Review_List(`ReviewID`,`Order`,`DateTime`) VALUES%s"%(insertsql)
        db.bmquery(hotelsql,'hive')

    if 0 < maxid:
        fp = open(reviewfile,'w+')
        fp.write(str(maxid))
        fp.close()


# if __name__ == "__main__":
#     AddDataInsertHive
    # hcoon = MySQLdb.connect(**savedb)
    # hcur = hcoon.cursor(cursorclass=MySQLdb.cursors.DictCursor)
    #
    # coonn = MySQLdb.connect(**ctripN)
    # curn = coonn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
    # NewDataInsert(curn,coonn,hcur,hcoon)
    # print "携程接口升级之后数据补充完成"

